layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //新闻列表
    var tableIns = table.render({
        elem: '#newsList',
        url : '/personal_blog/blogs/getBlogByPageDeleted',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        limits : [10,15,20,25],
        id : "newsListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', title: 'ID', width:60, align:"center"},
            {field: 'title', title: '文章标题', width:350},
            {field: 'newsAuthor', title: '发布者', align:'center',width:150},
            {field: 'newsStatus', title: '发布状态',  align:'center',templet:"#newsStatus",width:150},
            {field: 'newsLook', title: '浏览权限', align:'center',templet:function (d) {
                return (d.newsLook=="1"?"开放浏览":"私密浏览");
            }},
            {field: 'newsTop', title: '是否置顶', align:'center', templet:function(d){
                return '<input type="checkbox" name="newsTop" disabled lay-filter="newsTop" beanId="'+d.id+'" lay-skin="switch" lay-text="是|否" '+(d.newsTop=='1'?'checked':'')+'>'
            }},
            {field: 'createTime', title: '发布时间', align:'center', minWidth:110, templet:function(d){
                return moment(d.createTime).format('YYYY-MM-DD HH:mm:ss').substring(0,10);
            }},
            {title: '操作', templet:'#newsListBar',fixed:"right",align:"center"}
        ]]
    });


    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('newsListTable'),
            data = checkStatus.data,
            newsId = [];
        if(data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].id);
            }
            layer.confirm('确定删除选中的文章？', {icon: 3, title: '提示信息'}, function (index) {
                var url ="/personal_blog/blogs/deleteAll";
                axios.delete(url,{
                	params: {
                	     ids: newsId+""
                	    }
                }).then(function(res){
                	tableIns.reload();
                    layer.close(index);
                })
            })
        }else{
            layer.msg("请选择需要删除的文章");
        }
    })
    
    //批量还原
    $(".back_btn").click(function(){
        var checkStatus = table.checkStatus('newsListTable'),
        data = checkStatus.data,
        newsId = [];
    if(data.length > 0) {
        for (var i in data) {
            newsId.push(data[i].id);
        }
        console.log(newsId+"");
        layer.confirm('确定还原选中的文章？', {icon: 3, title: '提示信息'}, function (index) {
            var url ="/personal_blog/blogs/backAll";
            $.ajax({
                type:"put",
                data:{
                    ids:newsId
                },
                traditional:true,
                url:url,
                success:function () {
                    tableIns.reload();
                    layer.close(index);
                }
            })
        })
    }else{
        layer.msg("请选择需要还原的文章");
    }
})

    
    
    
    
    
    
    //列表操作
    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addNews(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此文章？',{icon:3, title:'提示信息'},function(index){
                var url = "/personal_blog/blogs/"+data.id;
                axios.delete(url).then(function (response) {
                    tableIns.reload();
                    layer.close(index);
                })
            });
        } else if(layEvent === 'back'){ //预览
        	layer.confirm('确定还原此文章？',{icon:3, title:'提示信息'},function(index){
                var url = "/personal_blog/blogs/back/"+data.id;
                axios.put(url).then(function (response) {
                    tableIns.reload();
                    layer.close(index);
                })
            });
        }
    });



    function getUrlParms(para){
        var search=location.search; //页面URL的查询部分字符串
        var arrPara=new Array(); //参数数组。数组单项为包含参数名和参数值的字符串，如“para=value”
        var arrVal=new Array(); //参数值数组。用于存储查找到的参数值

        if(search!=""){
            var index=0;
            search=search.substr(1); //去除开头的“?”
            arrPara=search.split("&");

            for(i in arrPara){
                var paraPre=para+"="; //参数前缀。即参数名+“=”，如“para=”
                if(arrPara[i].indexOf(paraPre)==0&& paraPre.length<arrPara[i].length){
                    arrVal[index]=decodeURI(arrPara[i].substr(paraPre.length)); //顺带URI解码避免出现乱码
                    index++;
                }
            }
        }

        if(arrVal.length==1){
            return arrVal[0];
        }else if(arrVal.length==0){
            return null;
        }else{
            return arrVal;
        }
    }







})